package net.mikoo.seals.manage.web.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.mikoo.seals.biz.service.ApplyServices;
import net.mikoo.seals.biz.service.DevicesActService;
import net.mikoo.seals.biz.service.DevicesServices;
import net.mikoo.seals.biz.service.DevicesTakeService;
import net.mikoo.seals.biz.service.DevicesUsedServices;
import net.mikoo.seals.biz.service.SealServices;
import net.mikoo.seals.biz.service.VerifyService;
import net.mikoo.seals.common.WebContext;
import net.mikoo.seals.common.enums.DevicesStatusEnum;
import net.mikoo.seals.common.enums.TakeStatusEnum;
import net.mikoo.seals.common.exception.ParametersException;
import net.mikoo.seals.common.model.PageModel;
import net.mikoo.seals.manage.web.common.BaseController;
import net.mikoo.seals.model.Apply;
import net.mikoo.seals.model.ApplyQuery;
import net.mikoo.seals.model.ApplyVerify;
import net.mikoo.seals.model.ApplyVerifyQuery;
import net.mikoo.seals.model.Devices;
import net.mikoo.seals.model.DevicesAct;
import net.mikoo.seals.model.DevicesActQuery;
import net.mikoo.seals.model.DevicesQuery;
import net.mikoo.seals.model.DevicesTake;
import net.mikoo.seals.model.DevicesTakeQuery;
import net.mikoo.seals.model.DevicesUsed;
import net.mikoo.seals.model.DevicesUsedCert;
import net.mikoo.seals.model.DevicesUsedQuery;
import net.mikoo.seals.model.Seal;
import net.mikoo.seals.model.SealQuery;
import net.mikoo.seals.model.Upfile;

@RequestMapping("/custos")
@Controller
public class CustosController extends BaseController {

	private static Logger logger = LoggerFactory.getLogger(CustosController.class);

	
	@Autowired
	private SealServices sealServices;
	
	@Autowired
	private DevicesServices devicesServices;
	
	@Autowired
	private ApplyServices applyServices;
	
	@Autowired
	private VerifyService verifyService;
	
	@Autowired
	private DevicesActService devicesActService;
	
	@Autowired
	private DevicesUsedServices devicesUsedServices;
	
	@Autowired
	private DevicesTakeService devicesTakeService;
	
	@RequestMapping("/index")
	public String index() {
		return "/custos/list";
	}
	
	@RequestMapping("/list")
	public String list(SealQuery query, HttpServletRequest request,Model model){
			Long partyId = getCurrentPartyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			
			query.setCustosPartyId(partyId);
			query.setCompanyId(companyId);
			query.setDevicesStatus(DevicesStatusEnum.ACTIVATE.code());
			
			PageModel<Seal> pm = sealServices.getPageByQuery(query);
			
			model.addAttribute("pm", pm);
			model.addAttribute("query", query);
			return "/custos/list";
			
	}
	
	/**
	 * 印章详情
	 * @param query
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/info")
	public String info(SealQuery query, HttpServletRequest request,Model model){
		Long partyId = getCurrentPartyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		
		query.setPartyId(partyId);
		query.setCompanyId(companyId);
		
		Seal seal = sealServices.getInfoByQuery(query);
		
		
		DevicesQuery dQuery = new DevicesQuery();
		dQuery.setCompanyId(companyId);
		dQuery.setSealId(query.getSealId());
		
		List<Devices> devicesList = devicesServices.getListByQuery(dQuery);
		seal.setDevicesList(devicesList);
		model.addAttribute("seal", seal);
		return "/custos/info";
		
	}
	
	@RequestMapping("/devicesList")
	public String devicesList(DevicesQuery query,HttpServletRequest request,Model model){
			Long partyId = getCurrentPartyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			query.setCompanyId(companyId);
			query.setCustosPartyId(partyId);
			
			PageModel<Devices> pm = devicesServices.getPageByQuery(query);
			model.addAttribute("pm", pm);
			return "/custos/devicesList";
	}
	
	@RequestMapping("/devicesInfo")
	public String devicesInfo(DevicesQuery query,HttpServletRequest request,Model model){
			Long partyId = getCurrentPartyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			query.setCompanyId(companyId);
			query.setCustosPartyId(partyId);
			
			Devices devices = devicesServices.getInfoByQuery(query);
			model.addAttribute("devices", devices);
			return "/custos/devicesInfo";
	}
	
	/**
	 * 申请成功可用次数
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/applyList")
	public String applyList(DevicesActQuery query,HttpServletRequest request,Model model){
			Long partyId = getCurrentPartyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			query.setCompanyId(companyId);
			query.setCustosPartyId(partyId);
			
			PageModel<DevicesAct> pm = devicesActService.getActPageByQuery(query);
			model.addAttribute("pm", pm);
			return "/custos/applyList";
	}
	
	@RequestMapping("/applyInfo")
	public String applyInfo(DevicesActQuery query,HttpServletRequest request,Model model){
			Long partyId = getCurrentPartyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			
			query.setCompanyId(companyId);
			query.setCustosPartyId(partyId);
			DevicesAct devicesAct = devicesActService.getInfoByQuery(query);
			model.addAttribute("devicesAct", devicesAct);
			
			ApplyQuery applyQuery = new ApplyQuery();
			applyQuery.setCompanyId(companyId);
			applyQuery.setApplyId(devicesAct.getApplyId());
			Apply apply = this.applyServices.getInfoByQuery(applyQuery);
			model.addAttribute("apply", apply);
			
			List<Upfile> applyFiles = applyServices.getApplyfiles(devicesAct.getApplyId());
			model.addAttribute("applyFiles", applyFiles);
			
			ApplyVerifyQuery verifyQuery  = new ApplyVerifyQuery();
			verifyQuery.setApplyId(devicesAct.getApplyId());
			verifyQuery.setCompanyId(companyId);
			List<ApplyVerify> verifys = verifyService.getListByQuery(verifyQuery);
			model.addAttribute("verifys", verifys);
			
			return "/custos/applyInfo";
	}
	
	
	
	/**
	 * 申请通过印章
	 * @param query
	 * @param request
	 * @param model
	 * @return
	 */
	
	@RequestMapping("/approveList")
	public String approveList(DevicesActQuery query, HttpServletRequest request, Model model) {

		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		Long partyId = getCurrentPartyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		query.setCompanyId(companyId);
		query.setPartyId(partyId);
		query.setCustosPartyId(partyId);
		PageModel<DevicesAct> pm = devicesActService.getActPageByQuery(query);
		model.addAttribute("pm", pm);
		model.addAttribute("query", query);
		
		return "custos/approveList";
	}
	
	
	
	@RequestMapping("/approveInfo")
	public String approveInfo(DevicesActQuery query, HttpServletRequest request, Model model) {

		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		query.setCompanyId(companyId);
		
//		query.setCompanyId(companyId);
		//query.setCustosPartyId(partyId);
		DevicesAct devicesAct = devicesActService.getInfoByQuery(query);
		model.addAttribute("devicesAct", devicesAct);
		
		ApplyQuery applyQuery = new ApplyQuery();
		applyQuery.setCompanyId(companyId);
		applyQuery.setApplyId(devicesAct.getApplyId());
		Apply apply = this.applyServices.getInfoByQuery(applyQuery);
		model.addAttribute("apply", apply);
		
		List<Upfile> applyFiles = applyServices.getApplyfiles(devicesAct.getApplyId());
		model.addAttribute("applyFiles", applyFiles);
		
		ApplyVerifyQuery verifyQuery  = new ApplyVerifyQuery();
		verifyQuery.setApplyId(devicesAct.getApplyId());
		verifyQuery.setCompanyId(companyId);
		List<ApplyVerify> verifys = verifyService.getListByQuery(verifyQuery);
		model.addAttribute("verifys", verifys);
		
		
		DevicesUsedQuery usedQuery = new DevicesUsedQuery();
		usedQuery.setCompanyId(companyId);
		usedQuery.setDevicesActId(devicesAct.getDevicesActId());
		List<DevicesUsed> useds = devicesUsedServices.getListByQuery(usedQuery);
		model.addAttribute("useds", useds);
		
		
		List<DevicesUsedCert> usedCerts = devicesUsedServices.getCertsByDevicesActId(devicesAct.getDevicesActId());
		model.addAttribute("usedCerts", usedCerts);
		
		
		
		return "custos/approveInfo";
	}
	
	
	@RequestMapping("/usedList")
	public String usedList(DevicesUsedQuery query,HttpServletRequest request,Model model){
			Long partyId = getCurrentPartyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			query.setCompanyId(companyId);
			query.setCustosPartyId(partyId);
			PageModel<DevicesUsed> pm = devicesUsedServices.getPageByQuery(query);
			model.addAttribute("pm", pm);
			return "/custos/usedList";
	}
	
	
	@RequestMapping("/usedInfo")
	public String usedInfo(DevicesUsedQuery query,HttpServletRequest request,Model model){
			
			Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			query.setCompanyId(companyId);
			
			DevicesUsed devicesUsed = devicesUsedServices.getInfoByQuery(query);
			List<DevicesUsedCert> usedCerts = devicesUsedServices.getCertsByQuery(query);
			model.addAttribute("used", devicesUsed);
			model.addAttribute("usedCerts", usedCerts);
			
			return "/custos/usedInfo";
	}
	
	
	
	@RequestMapping("/actCensor")
	@ResponseBody
	public Map<String,Object> actCensor(DevicesAct devicesAct,HttpServletRequest request,Model model){
		
		try {
			Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			Long partyId = getCurrentPartyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			devicesAct.setCompanyId(companyId);
			devicesAct.setCensorPartyId(partyId);
			devicesActService.censor(devicesAct);
			return result(SUCCESS, "操作成功");
			
		}catch (ParametersException e) {
			logger.error("",e);
			return result(ERROR, e.getMessage());
		}catch (Exception e) {
			logger.error("",e);
			return result(ERROR, "系统出错");
		}
		
	}
	
	
	@RequestMapping("/takeList")
	public String takeList(DevicesTakeQuery query,HttpServletRequest request,Model model){
			Long partyId = getCurrentPartyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			query.setCompanyId(companyId);
			query.setCustosPartyId(partyId);
			PageModel<DevicesTake> pm = devicesTakeService.getPageByQuery(query);
			model.addAttribute("pm", pm);
			return "/custos/takeList";
	}
	
	/**
	 * 确认领取设备
	 * @param devicesTake
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/take/receive")
	public String takeReceive(DevicesTake devicesTake,HttpServletRequest request,Model model){
			Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			devicesTake.setCompanyId(companyId);
			
			Long sealId = devicesTake.getSealId();
			SealQuery sealQuery = new SealQuery();
			sealQuery.setSealId(sealId);
			sealQuery.setCompanyId(companyId);
			Seal seal = sealServices.getInfoByQuery(sealQuery);
			model.addAttribute("seal", seal);
			
			DevicesQuery devicesQuery = new DevicesQuery();
			devicesQuery.setCompanyId(companyId);
			devicesQuery.setSealId(sealId);
			devicesQuery.setTakeStatus(TakeStatusEnum.NORMAL.code());
			devicesQuery.setDevicesStatus(DevicesStatusEnum.ACTIVATE.code());
			List<Devices> deviceses = devicesServices.getListByQuery(devicesQuery);
			model.addAttribute("deviceses", deviceses);
			
			model.addAttribute("devicesTake", devicesTake);
			return "/custos/receive";
	}
	
	@RequestMapping("/take/receive/submit")
	@ResponseBody
	public Map<String,Object> takeReceiveSubmit(DevicesTake devicesTake,HttpServletRequest request,Model model){
		
		try {
			Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			devicesTake.setCompanyId(companyId);
			devicesTakeService.receive(devicesTake);
			return result(SUCCESS, "操作成功");
			
		}catch (ParametersException e) {
			logger.error("",e);
			return result(ERROR, e.getMessage());
		}catch (Exception e) {
			logger.error("",e);
			return result(ERROR, "系统出错");
		}
		
	}
	
	
	/**
	 * 归还设备
	 * @param query
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/take/return")
	public String takeReturn(DevicesTake devicesTake,HttpServletRequest request,Model model){
		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		devicesTake.setCompanyId(companyId);
		
		Long sealId = devicesTake.getSealId();
		SealQuery sealQuery = new SealQuery();
		sealQuery.setSealId(sealId);
		sealQuery.setCompanyId(companyId);
		Seal seal = sealServices.getInfoByQuery(sealQuery);
		model.addAttribute("seal", seal);
		
		DevicesQuery devicesQuery = new DevicesQuery();
		devicesQuery.setCompanyId(companyId);
		devicesQuery.setSealId(sealId);
		devicesQuery.setDevicesId(devicesTake.getDevicesId());
		
		Devices devices = devicesServices.getInfoByQuery(devicesQuery);
		model.addAttribute("devices", devices);
		
		model.addAttribute("devicesTake", devicesTake);
		return "/custos/return";
	}
	
	
	@RequestMapping("/take/return/submit")
	@ResponseBody
	public Map<String,Object> takeReturnSubmit(DevicesTake devicesTake,HttpServletRequest request,Model model){
		
		try {
			Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			devicesTake.setCompanyId(companyId);
			devicesTakeService.returns(devicesTake);
			return result(SUCCESS, "操作成功");
			
		}catch (ParametersException e) {
			logger.error("",e);
			return result(ERROR, e.getMessage());
		}catch (Exception e) {
			logger.error("",e);
			return result(ERROR, "系统出错");
		}
		
	}
	
	
	
	@RequestMapping("/takeInfo")
	public String takeInfo(DevicesTakeQuery query,HttpServletRequest request,Model model){
			Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			query.setCompanyId(companyId);
			
			DevicesTake devicesTake = devicesTakeService.getInfoByQuery(query);
			model.addAttribute("devicesTake", devicesTake);
			
			DevicesQuery devicesQuery = new DevicesQuery();
			devicesQuery.setDevicesId(devicesTake.getDevicesId());
			devicesQuery.setCompanyId(companyId);
			Devices devices = devicesServices.getInfoByQuery(devicesQuery);
			model.addAttribute("devices", devices);
			
			
			
			
			return "/custos/takeInfo";
	}
	
	
	@RequestMapping("/detail")
	public String detailPage(ApplyQuery query, String type, HttpServletRequest request, Model model) {
		if("used".equals(type)) {
			DevicesUsedQuery devicesUsedQuery = new DevicesUsedQuery();
			devicesUsedQuery.setDevicesId(query.getDevicesId());
			devicesUsedQuery.setOffset(query.getOffset());
			devicesUsedQuery.setPageSize(query.getPageSize());
			PageModel<DevicesUsed> pm = devicesUsedServices.getPageByQuery(devicesUsedQuery);
			model.addAttribute("pm", pm);
			model.addAttribute("type", "used");
		}else {
			PageModel<Apply> pm = applyServices.getPageByQuery(query);
			model.addAttribute("pm", pm);
			model.addAttribute("type", "apply");
		}
		Devices devices = devicesServices.getInfoById(query.getDevicesId());
		model.addAttribute("devices", devices);
		return "/custos/detail";
	}
	
	
}
